<?php

namespace app\store\controller\apps\dealer;

use app\store\controller\Controller;
use app\common\service\qrcode\Poster;
use app\store\model\dealer\User as UserModel;
use app\store\model\dealer\Referee as RefereeModel;
use app\store\model\dealer\Setting as SettingModel;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use app\store\service\order\Export as Exportservice;
/**
 * 分销商管理
 * Class User
 * @package app\store\controller\apps\dealer
 */
class User extends Controller
{
    /**
     * 分销商导出
     *
     * Undocumented function long description
     *
     * @param Type $var Description
     * @return type
     * @throws conditon
     **/
    public function export($search = '',$start_time = '' ,$end_time = '', $sort_order = 'desc')
    {
        $model = new UserModel;
        $data = $model->getList($search,$start_time ,$end_time,$sortOrder = 'desc',$pagesite = 111111111111111);
        // 设置表头
        $headers = ['微信昵称','姓名', '手机号', '累计推荐人数', '添加人', '成为时间'];
        return (new Exportservice)->delist($headers,$data);
    }
    /**
     * 构造方法
     * @throws \app\common\exception\BaseException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 分销商用户列表
     * @param string $search
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($search = '',$start_time = '' ,$end_time = '', $sort_order = 'desc')
    {
        $model = new UserModel;
        // dump($model->getList($search,$start_time ,$end_time));die;
        return $this->fetch('index', [
            'list' => $model->getList($search,$start_time ,$end_time, $sort_order),
            'basicSetting' => SettingModel::getItem('basic'),
        ]);
    }

    /**
     * 分销商用户列表
     * @param string $user_id
     * @param int $level
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function fans($user_id, $level = -1)
    {
        $model = new RefereeModel;
        return $this->fetch('fans', [
            'list' => $model->getList($user_id, $level),
            'basicSetting' => SettingModel::getItem('basic'),
        ]);
    }

    /**
     * 编辑分销商
     * @param $dealer_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function edit($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        if (!$this->request->isAjax()) {
            return $this->fetch('edit', compact('model'));
        }
        if ($model->edit($this->postData('model'))) {
            return $this->renderSuccess('更新成功', url('apps.dealer.user/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除分销商
     * @param $dealer_id
     * @return array|bool
     * @throws \think\exception\DbException
     */
    public function delete($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        if (!$model->setDelete()) {
            return $this->renderError('删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 分销商二维码
     * @param $dealer_id
     * @throws \app\common\exception\BaseException
     * @throws \think\exception\DbException
     * @throws \Exception
     */
    public function qrcode($dealer_id)
    {
        $model = UserModel::detail($dealer_id);
        $Qrcode = new Poster($model);
        $this->redirect($Qrcode->getImage());
    }

}